﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Sorting.App.Controls
{
    /// <summary>
    /// MonitoringBlock.xaml 的交互逻辑
    /// </summary>
    public partial class MonitoringBlock : UserControl
    {
        public MonitoringBlock()
        {
            InitializeComponent();
        }

        public short MonValue
        {
            get { return (short)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }


        public string MonText
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }


        // Using a DependencyProperty as the backing store for Items.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register(nameof(MonValue), typeof(short), typeof(MonitoringBlock),
                new PropertyMetadata((short)0, PropertyChangedCallback, null));



        public static readonly DependencyProperty TextProperty =
         DependencyProperty.Register(nameof(MonText), typeof(string), typeof(MonitoringBlock),
             new PropertyMetadata("", TextPropertyChangedCallback, null));


        private static void PropertyChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue is short newValue)
            {
                var control = obj as MonitoringBlock;
                if ((short)args.NewValue==0)
                {


                    control.Border.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#3498db"));
                    control.txtInfo.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#000000"));
             
                }
                else if ((short)args.NewValue == 1)
                {
                    control.Border.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2ecc71"));
                    control.txtInfo.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFFFF"));
                }
                else
                {

                    control.Border.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ff4757"));
                    control.txtInfo.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#000000"));
                }

            }
        }


        private static void TextPropertyChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue is string newValue)
            {
                var control = obj as MonitoringBlock;
                control.txtInfo.Text = args.NewValue.ToString();

            }
        }

        //public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(pText), typeof(string),
        //    typeof(UserControl1), new PropertyMetadata(defaultValue: "666"));
        //public string pText
        //{
        //    get { return (string)GetValue(TextProperty); }
        //    set { SetValue(TextProperty, value); }
        //}

    }
}
